//package com.yunkaweilai.android.utils;
//
//import android.content.Context;
//import android.widget.Toast;
//
//import com.yunkaweilai.android.model.home.RecordListBean;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.ArrayList;
//import java.util.List;
//
//import jxl.Workbook;
//import jxl.WorkbookSettings;
//import jxl.format.Colour;
//import jxl.write.Label;
//import jxl.write.WritableCell;
//import jxl.write.WritableCellFormat;
//import jxl.write.WritableFont;
//import jxl.write.WritableSheet;
//import jxl.write.WritableWorkbook;
//import jxl.write.WriteException;
//
///**
// * 数据导出excel
// *
// * @author dmrfcoder
// * @date 2018/8/9
// */
//public class ExcelUtils {
//    private static WritableFont arial14font = null;
//
//    private static WritableCellFormat arial14format = null;
//    private static WritableFont arial10font = null;
//    private static WritableCellFormat arial10format = null;
//    private static WritableFont arial12font = null;
//    private static WritableCellFormat arial12format = null;
//    private final static String UTF8_ENCODING = "UTF-8";
//
//    /**
//     * 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...
//     */
//    private static void format() {
//        try {
//            arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
//            arial14font.setColour(jxl.format.Colour.LIGHT_BLUE);
//            arial14format = new WritableCellFormat(arial14font);
//            arial14format.setAlignment(jxl.format.Alignment.CENTRE);
//            arial14format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
//            arial14format.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW);
//
//            arial10font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
//            arial10format = new WritableCellFormat(arial10font);
//            arial10format.setAlignment(jxl.format.Alignment.CENTRE);
//            arial10format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
//            arial10format.setBackground(Colour.GRAY_25);
//
//            arial12font = new WritableFont(WritableFont.ARIAL, 10);
//            arial12format = new WritableCellFormat(arial12font);
//            //对齐格式
//            arial10format.setAlignment(jxl.format.Alignment.CENTRE);
//            //设置边框
//            arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
//
//        } catch (WriteException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 初始化Excel
//     *
//     * @param fileName 导出excel存放的地址（目录）
//     * @param colName  excel中包含的列名（可以有多个）
//     */
//    public static void initExcel(String fileName, String[] colName) {
//        format();
//        WritableWorkbook workbook = null;
//        try {
//            File file = new File(fileName);
//            if (!file.exists()) {
//                file.createNewFile();
//            }
//            workbook = Workbook.createWorkbook(file);
//            //设置表格的名字
//            WritableSheet sheet = workbook.createSheet("账单", 0);
//            //创建标题栏
//            sheet.addCell((WritableCell) new Label(0, 0, fileName, arial14format));
//            for (int col = 0; col < colName.length; col++) {
//                sheet.addCell(new Label(col, 0, colName[col], arial10format));
//            }
//            //设置行高
//            sheet.setRowView(0, 340);
//            workbook.write();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (workbook != null) {
//                try {
//                    workbook.close();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
//
//    /**
//     * 初始化Excel表格
//     *
//     * @param filePath  存放excel文件的路径（path/demo.xls）
//     * @param sheetName Excel表格的表名
//     * @param colName   excel中包含的列名（可以有多个）
//     */
//    public static void initExcel(String filePath, String sheetName, String[] colName) {
//        format();
//        WritableWorkbook workbook = null;
//        try {
//            File file = new File(filePath);
//            if (!file.exists()) {
//                file.createNewFile();
//            } else {
//                return;
//            }
//            workbook = Workbook.createWorkbook(file);
//            //设置表格的名字
//            WritableSheet sheet = workbook.createSheet(sheetName, 0);
//            //创建标题栏
//            sheet.addCell((WritableCell) new Label(0, 0, filePath, arial14format));
//            for (int col = 0; col < colName.length; col++) {
//                sheet.addCell(new Label(col, 0, colName[col], arial10format));
//            }
//            //设置行高
//            sheet.setRowView(0, 340);
//            workbook.write();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (workbook != null) {
//                try {
//                    workbook.close();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
//
//
//    @SuppressWarnings("unchecked")
//    public static <T> void writeObjListToExcel(List<T> objList, String fileName, Context context) {
//        if (objList != null && objList.size() > 0) {
//            WritableWorkbook writebook = null;
//            InputStream in = null;
//            try {
//                WorkbookSettings setEncode = new WorkbookSettings();
//                setEncode.setEncoding(UTF8_ENCODING);
//                in = new FileInputStream(new File(fileName));
//                Workbook workbook = Workbook.getWorkbook(in);
//                writebook = Workbook.createWorkbook(new File(fileName), workbook);
//                WritableSheet sheet = writebook.getSheet(0);
//
//                for (int j = 0; j < objList.size(); j++) {
//                    RecordListBean projectBean = (RecordListBean) objList.get(j);
//                    List<String> list = new ArrayList<>();
//                    list.add(projectBean.getData().getTitleName());
//                    list.add(projectBean.getData().getOrder_spayment());
//
//                    for (int i = 0; i < list.size(); i++) {
//                        sheet.addCell(new Label(i, j + 1, list.get(i), arial12format));
//                        if (list.get(i).length() <= 4) {
//                            //设置列宽
//                            sheet.setColumnView(i, list.get(i).length() + 8);
//                        } else {
//                            //设置列宽
//                            sheet.setColumnView(i, list.get(i).length() + 5);
//                        }
//                    }
//                    //设置行高
//                    sheet.setRowView(j + 1, 350);
//                }
//
//                writebook.write();
//                Toast.makeText(context, "导出Excel成功", Toast.LENGTH_SHORT).show();
//            } catch (Exception e) {
//                e.printStackTrace();
//            } finally {
//                if (writebook != null) {
//                    try {
//                        writebook.close();
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//
//                }
//                if (in != null) {
//                    try {
//                        in.close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//
//        }
//    }
//}
//
////public class MainActivity extends Activity implements View.OnClickListener {
////
////    private Button exportButton;
////    private Button openButton;
////    private TextView textView;
////
////    private AlertDialog alertDialog;
////    private AlertDialog mDialog;
////
////
////    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
////
////
////    private int REQUEST_PERMISSION_CODE = 1000;
////
////
////    private String filePath = "/sdcard/AndroidExcelDemo";
////
////    private void requestPermission() {
////        if (Build.VERSION.SDK_INT > 23) {
////            if (ContextCompat.checkSelfPermission(MainActivity.this,
////                    permissions[0])
////                    == PackageManager.PERMISSION_GRANTED) {
////                //授予权限
////                Log.i("requestPermission:", "用户之前已经授予了权限！");
////            } else {
////                //未获得权限
////                Log.i("requestPermission:", "未获得权限，现在申请！");
////                requestPermissions(permissions
////                        , REQUEST_PERMISSION_CODE);
////            }
////        }
////
////    }
////
////    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
////    @Override
////    protected void onCreate(Bundle savedInstanceState) {
////        super.onCreate(savedInstanceState);
////        setContentView(R.layout.activity_main);
////        requestPermission();
////        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
////        StrictMode.setVmPolicy(builder.build());
////        builder.detectFileUriExposure();
////
////
////        exportButton = findViewById(R.id.export_button);
////        exportButton.setOnClickListener(this);
////
////        openButton = findViewById(R.id.open_button);
////        openButton.setOnClickListener(this);
////        textView = findViewById(R.id.textView);
////
////
////    }
////
////    @Override
////    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
////        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
////
////        if (requestCode == REQUEST_PERMISSION_CODE) {
////            if (grantResults.length > 0
////                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
////                Log.i("onPermissionsResult:", "权限" + permissions[0] + "申请成功");
////                // permission was granted, yay! Do the
////                // contacts-related task you need to do.
////
////            } else {
////                Log.i("onPermissionsResult:", "用户拒绝了权限申请");
////                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
////                builder.setTitle("permission")
////                        .setMessage("点击允许才可以使用我们的app哦")
////                        .setPositiveButton("去允许", new DialogInterface.OnClickListener() {
////                            @Override
////                            public void onClick(DialogInterface dialog, int id) {
////                                if (alertDialog != null && alertDialog.isShowing()) {
////                                    alertDialog.dismiss();
////                                }
////                                ActivityCompat.requestPermissions(MainActivity.this,
////                                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
////                            }
////                        });
////                alertDialog = builder.create();
////                alertDialog.setCanceledOnTouchOutside(false);
////                alertDialog.show();
////                // permission denied, boo! Disable the
////                // functionality that depends on this permission.
////            }
////        }
////    }
////
////    private void showDialogTipUserRequestPermission() {
////        ActivityCompat.requestPermissions(this, permissions, 321);
////    }
////
////    @Override
////    public void onClick(View view) {
////        switch (view.getId()) {
////            case R.id.export_button:
////                exportExcel(this);
////
////                break;
////            case R.id.open_button:
////                openDir();
////            default:
////                break;
////        }
////    }
////
////    private void openDir() {
////
////        File file = new File(filePath);
////        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
////        intent.setType("*/*");
////        intent.addCategory(Intent.CATEGORY_OPENABLE);
////        intent.setDataAndType(Uri.fromFile(file), "file/*");
////        try {
////            startActivity(intent);
////        } catch (Exception e) {
////            Toast.makeText(this, "没有正确打开文件管理器", Toast.LENGTH_SHORT).show();
////        }
////    }
////
////
////    private void exportExcel(Context context) {
////
////
////        File file = new File(filePath);
////        if (!file.exists()) {
////            file.mkdirs();
////        }
////
////
////        String excelFileName = "/demo.xls";
////
////
////        String[] title = {"姓名", "年龄", "男孩"};
////        String sheetName = "demoSheetName";
////
////
////        List<DemoBean> demoBeanList = new ArrayList<>();
////        DemoBean demoBean1 = new DemoBean("张三", 10, true);
////        DemoBean demoBean2 = new DemoBean("小红", 12, false);
////        DemoBean demoBean3 = new DemoBean("李四", 18, true);
////        DemoBean demoBean4 = new DemoBean("王香", 13, false);
////        demoBeanList.add(demoBean1);
////        demoBeanList.add(demoBean2);
////        demoBeanList.add(demoBean3);
////        demoBeanList.add(demoBean4);
////        filePath = filePath + excelFileName;
////
////
////        ExcelUtil.initExcel(filePath, sheetName, title);
////
////
////        ExcelUtil.writeObjListToExcel(demoBeanList, filePath, context);
////
////        textView.setText("excel已导出至：" + filePath);
////
////    }
////}